View Javadoc

1   package net.sf.mmapps.applications.developer;
2   
3   import java.awt.*;
4   import org.apache.commons.logging.*;
5   /***
6    * table layout will act the same as GridLayout width two differances
7    * the size of the differen rows is variable
8    * <pre>
9    *  ------------------------------------
10   *  | label | content                  |
11   *  ------------------------------------
12   *  | label | the large content        |
13   *  |       | component                |
14   *  ------------------------------------
15   * </pre>
16   * the size depends on the size of the components
17   * @author Kees Jongenburger
18   * @version $Id: TableLayout.java,v 1.2 2004/07/16 20:55:48 keesj Exp $
19   **/
20  public class TableLayout implements LayoutManager{
21      private static Log log = LogFactory.getLog(TableLayout.class);
22  
23      int width;
24      /***
25       * @param width width in "pieces"
26       **/
27      public TableLayout(int width){
28          this.width = width;
29      }
30      
31      public void layoutContainer(Container container){
32          synchronized (container.getTreeLock()) {
33              int componentCount = container.getComponentCount();
34              Dimension[] dims = new Dimension[componentCount];
35              //get all the preferredSizes and put them in an array
36              for (int x = 0 ; x < componentCount ; x++){
37                  Component comp = container.getComponent(x);
38                  dims[x] = comp.getPreferredSize();
39              }
40              
41              int[] widths = new int[width];
42              //for each row find the width
43              for (int x = 0 ; x < componentCount ; x++){
44                  int compwidth = dims[x].width;
45                  
46                  if (compwidth > widths[ x % width]){
47                      widths[x % width] = compwidth;
48                      log.debug("setting width["+ (x %width) +"] to "+ compwidth +".");
49                  }
50                  
51              }
52              int y =0;
53              int maxY =0 ;
54              for (int x =0 ; x < componentCount ; x++){
55                  int startX =0;
56                  for (int startXCounter =0 ; startXCounter < x %width; startXCounter ++){
57                      startX += widths[startXCounter];
58                  }
59                  if (maxY < dims[x].height){
60                      maxY = dims[x].height;
61                  }
62                  if (x != 0 && x % width  == 0) {
63                      y += maxY;
64                      maxY = 0;
65                  }
66                  Component comp = container.getComponent(x);
67                  comp.setBounds(startX,y,dims[x].width,dims[x].height);
68                  log.debug("setting startx of component number "+ x +" to "+ startX +" y is "+ y +".");
69                  
70                  
71              }
72          }
73      };
74      
75      public Dimension getSize(Container container){
76          int componentCount = container.getComponentCount();
77          Dimension[] dims = new Dimension[componentCount];
78          //get all the preferredSizes and put them in an array
79          for (int x = 0 ; x < componentCount ; x++){
80              Component comp = container.getComponent(x);
81              dims[x] = comp.getPreferredSize();
82          }
83          
84          int[] widths = new int[width];
85          //for each row find the width by searching down and finding the widest
86          // component
87          for (int x = 0 ; x < componentCount ; x++){
88              int compwidth = dims[x].width;
89              
90              if (compwidth > widths[ x % width]){
91                  widths[x % width] = compwidth;
92                  log.debug("setting width["+ (x %width) +"] to row "+ compwidth +".");
93              }
94          }
95          
96          int[] heights = new int[(componentCount / width) + 1];
97          //for each row find the width by searching down and finding the widest
98          // component of a row and adding it to the higest
99          for (int x = 0 ; x < componentCount ; x++){
100             int compheight = dims[x].height;
101             if (compheight > heights[ (x / width) ]){
102                 heights[(x / width)] = compheight;
103                 log.debug("setting height["+ compheight +"] to row "+ (x / width) +".");
104             }
105         }
106         
107         int y =0;
108         for (int heightCounter =0 ; heightCounter < (componentCount / width) + 1; heightCounter ++){
109             y += heights[heightCounter];
110         }
111         
112         int x = 0;
113         for (int k = 0; k < width;k++){
114             x =+ widths[k];
115         }
116         
117         return new Dimension(x, y);
118     }
119     
120     public void addLayoutComponent(String data,Component component){
121         log.debug("addLayoutComponent");
122     };
123     
124     public void removeLayoutComponent(Component component){
125         log.debug("removeLayoutComponent");
126     };
127     
128     public Dimension preferredLayoutSize(Container container){
129         return getSize(container);
130     };
131     
132     public Dimension minimumLayoutSize(Container container){
133         return getSize(container);
134     };
135 }